package com.zxjbyte.yiyi.module.system.controller.dict;

import com.zxjbyte.yiyi.framework.common.domain.KVStore;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.domain.api.R;
import com.zxjbyte.yiyi.framework.common.util.CollXUtil;
import com.zxjbyte.yiyi.module.system.domain.entity.DictItemPO;
import com.zxjbyte.yiyi.module.system.domain.param.dict.DictItemAddParam;
import com.zxjbyte.yiyi.module.system.domain.param.dict.DictItemListParam;
import com.zxjbyte.yiyi.module.system.domain.param.dict.DictItemUpdateParam;
import com.zxjbyte.yiyi.module.system.domain.vo.DictItemVO;
import com.zxjbyte.yiyi.module.system.manager.DictManager;
import com.zxjbyte.yiyi.module.system.service.DictItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zhangxingjia
 * @Date 2024/1/24 16:14
 * @Version: 1.0
 */
@Slf4j
@Tag(name = "字典项接口")
@RestController
@RequestMapping(value = "/system/dict-item")
public class DictItemController {

    @Resource
    private DictItemService dictItemService;
    @Resource
    private DictManager dictManager;

    @Operation(summary = "字典项分页查询")
    @GetMapping("/list")
    public PageResult<DictItemVO> list(@ParameterObject @Valid DictItemListParam param){
        return dictItemService.list(param);
    }

    @Operation(summary = "字典项详情查询")
    @GetMapping("/detail")
    public R<DictItemVO> detail(@Parameter(description = "唯一id" ) @RequestParam Long id){
        return R.data(dictItemService.detail(id));
    }

    @Operation(summary = "新增字典项")
    @PostMapping("/add")
    public R<?> add(@Valid @RequestBody DictItemAddParam param){
        dictManager.saveDictItem(param);
        return R.ok();
    }

    @Operation(summary = "编辑字典项")
    @PutMapping("/update")
    public R<?> update(@Valid @RequestBody DictItemUpdateParam param){
        dictManager.updateDictItem(param);
        return R.ok();
    }

    @Operation(summary = "删除字典项")
    @DeleteMapping("/del")
    public R<?> delete(@Parameter(description = "唯一id" ) @RequestParam Long id){
        dictManager.deleteDictItem(id);
        return R.ok();
    }

    @Operation(summary = "字典数据下拉框")
    @GetMapping("/dropdown")
    public R<List<KVStore>> dropdown(@Parameter(description = "字典id") @RequestParam Long dictId){
        List<DictItemPO> list = dictItemService.listByDictId(dictId);
        if(CollXUtil.isNotEmpty(list)){
            List<KVStore> resultList = list.stream().map(s -> KVStore.create().set("value", s.getItemValue()).set("label", s.getItemLabel()))
                    .collect(Collectors.toList());
            return R.data(resultList);
        }
        return R.ok();
    }
}
